package com.youkeyi.ddy.cloud.application.business.hr.employee.contract.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.contract.BusinessEmployeeContractCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.contract.dto.BusinessEmployeeContractListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.contract.dto.BusinessEmployeeContractListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.contract.param.BusinessEmployeeContractCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.contract.param.BusinessEmployeeContractDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.contract.param.BusinessEmployeeContractListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.contract.param.BusinessEmployeeContractUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.HrEmployeeContractCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.dto.HrEmployeeContractListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.dto.HrEmployeeContractListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.param.HrEmployeeContractCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.param.HrEmployeeContractDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.param.HrEmployeeContractQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.param.HrEmployeeContractUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.HrEmployeeCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.dto.HrEmployeeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.dto.HrEmployeeListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.param.HrEmployeeQueryCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * BusinessEmployeeContractController
 *
 * @Author liuxiawang
 * @Date 2024/4/29 5:40 PM
 * @Location shenzhen.china
 */
@Service
public class BusinessEmployeeContractController implements BusinessEmployeeContractCloudService {
    
    @Autowired
    private HrEmployeeContractCloudService hrEmployeeContractCloudService;
    @Autowired
    private HrEmployeeCloudService hrEmployeeCloudService;

    @Override
    public BusinessEmployeeContractListCloudDto list(BusinessEmployeeContractListCloudParam param) {
        BusinessEmployeeContractListCloudDto dto = new BusinessEmployeeContractListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (StringUtils.isNotBlank(param.getNameFuzzy()) || CollectionUtils.isNotEmpty(param.getEmployeeCodes())) {
            HrEmployeeQueryCloudParam employeeQueryCloudParam = new HrEmployeeQueryCloudParam();
            employeeQueryCloudParam.setEmployeePrimaryKeys(param.getEmployeePrimaryKeys());
            employeeQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
            employeeQueryCloudParam.setEmployeeCodes(param.getEmployeeCodes());
            employeeQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
            HrEmployeeListCloudDto employeeDtos = hrEmployeeCloudService.query(employeeQueryCloudParam);
            if (null == employeeDtos || CollectionUtils.isEmpty(employeeDtos.getRows())) {
                dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
                return dto;
            }

            param.getEmployeePrimaryKeys().clear();
            param.getEmployeePrimaryKeys().addAll(employeeDtos.getRows().stream().map(HrEmployeeListItemCloudDto::getEmployeePrimaryKey).collect(Collectors.toList()));
        }

        HrEmployeeContractQueryCloudParam employeeQueryCloudParam = new HrEmployeeContractQueryCloudParam();
        employeeQueryCloudParam.setEmployeeContractPrimaryKeys(param.getEmployeeContractPrimaryKeys());
        employeeQueryCloudParam.setEmployeePrimaryKeys(param.getEmployeePrimaryKeys());
        employeeQueryCloudParam.setPageSize(param.getPageSize());
        employeeQueryCloudParam.setCurrentPage(param.getCurrentPage());
        HrEmployeeContractListCloudDto employeeDtos = hrEmployeeContractCloudService.query(employeeQueryCloudParam);
        if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
            HrEmployeeQueryCloudParam eqcp = new HrEmployeeQueryCloudParam();
            eqcp.setEmployeePrimaryKeys(employeeDtos.getRows().stream().map(HrEmployeeContractListItemCloudDto::getEmployeeKey).collect(Collectors.toList()));
            HrEmployeeListCloudDto eqcpDtos = hrEmployeeCloudService.query(eqcp);
            Map<String, HrEmployeeListItemCloudDto> eMap = new HashMap<>();
            if (null != eqcpDtos && CollectionUtils.isNotEmpty(eqcpDtos.getRows())) {
                eMap.putAll(eqcpDtos.getRows().stream().collect(Collectors.toMap(HrEmployeeListItemCloudDto::getEmployeePrimaryKey, Function.identity(), (k1, k2) -> k1)));
            }

            List<BusinessEmployeeContractListItemCloudDto> items = new ArrayList<>();
            employeeDtos.getRows().forEach(employee -> {
                BusinessEmployeeContractListItemCloudDto item = new BusinessEmployeeContractListItemCloudDto();
                item.setEmployeeContractPrimaryKey(employee.getEmployeeContractPrimaryKey());
                item.setEmployeeKey(employee.getEmployeeKey());
                if (eMap.containsKey(employee.getEmployeeKey())) {
                    HrEmployeeListItemCloudDto e = eMap.get(employee.getEmployeeKey());
                    item.setEmployeeName(e.getName());
                    item.setEmployeeCode(e.getEmployeeCode());
                }
                item.setStartDate(employee.getStartDate());
                item.setEndDate(employee.getEndDate());
                item.setContractName(employee.getContractName());
                item.setContractType(employee.getContractType());
                item.setSignedFlag(employee.getSignedFlag());
                item.setContractStatus(employee.getContractStatus());
                item.setAnnexKey(employee.getAnnexKey());
                item.setIsDeleted(employee.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(employee.getCreateTime());
                item.setCreateUserKey(employee.getCreateUserKey());
                item.setCreateUserName(employee.getCreateUserName());
                item.setUpdateTime(employee.getUpdateTime());
                item.setUpdateUserKey(employee.getUpdateUserKey());
                item.setUpdateUserName(employee.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(employeeDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessEmployeeContractCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeeKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeContractCreateCloudParam employeeCreateCloudParam = new HrEmployeeContractCreateCloudParam();
        employeeCreateCloudParam.setEmployeeKey(param.getEmployeeKey());
        employeeCreateCloudParam.setStartDate(param.getStartDate());
        employeeCreateCloudParam.setEndDate(param.getEndDate());
        employeeCreateCloudParam.setContractName(param.getContractName());
        employeeCreateCloudParam.setContractType(param.getContractType());
        employeeCreateCloudParam.setSignedFlag(param.getSignedFlag());
        employeeCreateCloudParam.setContractStatus(param.getContractStatus());
        employeeCreateCloudParam.setAnnexKey(param.getAnnexKey());
        employeeCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeContractCloudService.create(employeeCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessEmployeeContractUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeeContractPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeContractUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeContractUpdateCloudParam();
        employeeUpdateCloudParam.setEmployeeContractPrimaryKey(param.getEmployeeContractPrimaryKey());
        employeeUpdateCloudParam.setStartDate(param.getStartDate());
        employeeUpdateCloudParam.setEndDate(param.getEndDate());
        employeeUpdateCloudParam.setContractName(param.getContractName());
        employeeUpdateCloudParam.setContractType(param.getContractType());
        employeeUpdateCloudParam.setSignedFlag(param.getSignedFlag());
        employeeUpdateCloudParam.setContractStatus(param.getContractStatus());
        employeeUpdateCloudParam.setAnnexKey(param.getAnnexKey());
        employeeUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeContractCloudService.update(employeeUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessEmployeeContractDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getEmployeeContractPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeContractDeleteCloudParam employeeDeleteCloudParam = new HrEmployeeContractDeleteCloudParam();
        employeeDeleteCloudParam.setEmployeeContractPrimaryKeys(param.getEmployeeContractPrimaryKeys());
        employeeDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeContractCloudService.delete(employeeDeleteCloudParam);
    }
}
